﻿<cfcomponent output="false">
	<cfset this.DSN = "Pathfinder">

	<cffunction name="GetEnhancementBonus" access="remote" output="false" returntype="any">
		<cfargument name="ItemCategoryID" type="numeric" required="true"><!--- 1 = minor | 2 = medium | 3 = major --->
		<cfargument name="ItemTypeID" type="numeric" required="true"><!--- 1 = Armor/Shields | 2 = weapons --->
		<cfargument name="Iterations" type="numeric" required="false" default="5000">

		<cfset local.CollatedItemTypes = [
			0,
			0,
			0,
			0,
			0,
			0,
			0
		]>

		<cftry>
			<cfloop from="1" to="#arguments.Iterations#" index="local.i">
				<cfif arguments.ItemTypeID eq 1>
					<cfquery name="local.GetItemType" datasource="#this.DSN#">
						select EnhancementBonus, Special
						from magicarmorandshields
						where categoryid = #arguments.ItemCategoryID#
						order by -LOG(1 - rand()) / weighted asc
						limit 1
					</cfquery>
				<cfelse>
					<cfquery name="local.GetItemType" datasource="#this.DSN#">
						select mw.EnhancementBonus, mw.Special
						from magicweapons mw
						where mw.categoryid = #arguments.ItemCategoryID#
						order by -LOG(1 - rand()) / mw.weighted asc
						limit 1
					</cfquery>
				</cfif>
				<cfif local.GetItemType.Special is "Specific">
					<cfset local.CollatedItemTypes[6]++>
				<cfelseif local.GetItemType.Special is "Special Ability">
					<cfset local.CollatedItemTypes[7]++>
				<cfelse>
					<cfset local.CollatedItemTypes[local.GetItemType.EnhancementBonus]++>
				</cfif>
			</cfloop>

			<cfset local.Results = CollatedItemTypes>
			<cfcatch>
				<cfset local.Results = Duplicate(cfcatch)>
			</cfcatch>
		</cftry>

		<cfreturn local.Results>
	</cffunction>

	<cffunction name="GetSpecialAbilities" access="remote" output="false" returntype="any">
		<cfargument name="ItemCategoryID" type="numeric" required="true"><!--- 1 = minor | 2 = medium | 3 = major --->
		<cfargument name="ItemTypeID" type="numeric" required="true"><!--- 1 = Armor | 2 = Shields | 3 = melee weapons | 4 = ranged weapons --->
		<cfargument name="Iterations" type="numeric" required="false" default="5000">

		<cfset local.CollatedItemTypes = {}>

		<cftry>
			<cfloop from="1" to="#arguments.Iterations#" index="local.i">
				<cfif arguments.ItemTypeID eq 1>
					<cfquery name="local.GetItemType" datasource="#this.DSN#">
						select sa.name as ability
						from specialabilities sa
						inner join magicarmorspecialabilitiesweightedlookup masawl on masawl.specialabilityid = sa.id
						where masawl.CategoryID = #arguments.ItemCategoryID#
						order by -LOG(1 - rand()) / masawl.weighted asc
						limit 1
					</cfquery>
				<cfelseif arguments.ItemTypeID eq 2>
					<cfquery name="local.GetItemType" datasource="#this.DSN#">
						select sa.name as ability
						from specialabilities sa
						inner join magicshieldspecialabilitiesweightedlookup mssawl on mssawl.specialabilityid = sa.id
						where mssawl.CategoryID = #arguments.ItemCategoryID#
						order by -LOG(1 - rand()) / mssawl.weighted asc
						limit 1
					</cfquery>
				<cfelseif arguments.ItemTypeID eq 3>
					<cfquery name="local.GetItemType" datasource="#this.DSN#">
						select sa.name as ability
						from specialabilities sa
						inner join magicweaponspecialabilitiesweightedlookup mwsawl on mwsawl.specialabilityid = sa.id
						where mwsawl.WeaponTypeID = 1 and mwsawl.CategoryID = #arguments.ItemCategoryID#
						order by -LOG(1 - rand()) / mwsawl.weighted asc
						limit 1
					</cfquery>
				<cfelse>
					<cfquery name="local.GetItemType" datasource="#this.DSN#">
						select sa.name as ability
						from specialabilities sa
						inner join magicweaponspecialabilitiesweightedlookup mwsawl on mwsawl.specialabilityid = sa.id
						where mwsawl.WeaponTypeID = 2 and mwsawl.CategoryID = #arguments.ItemCategoryID#
						order by -LOG(1 - rand()) / mwsawl.weighted asc
						limit 1
					</cfquery>
				</cfif>

				<cfif StructKeyExists(local.CollatedItemTypes, local.GetItemType.Ability)>
					<cfset local.CollatedItemTypes[local.GetItemType.Ability]++>
				<cfelse>
					<cfset local.CollatedItemTypes[local.GetItemType.Ability] = 1>
				</cfif>

			</cfloop>

			<cfset local.Results = CollatedItemTypes>
			<cfcatch>
				<cfset local.Results = Duplicate(cfcatch)>
			</cfcatch>
		</cftry>

		<cfreturn local.Results>
	</cffunction>

	<cffunction name="GetWeaponCategory" access="remote" output="false" returntype="any">
		<cfargument name="Iterations" type="numeric" required="false" default="5000">

		<cfset local.CollatedItemTypes = [
			0,
			0,
			0
		]>

		<cftry>
			<cfloop from="1" to="#arguments.Iterations#" index="local.i">
				<cfquery name="local.GetItemType" datasource="#this.DSN#">
					select id from weaponcategories order by -LOG(1 - rand()) / weighted asc limit 1
				</cfquery>

				<cfset local.CollatedItemTypes[local.GetItemType.ID]++>
			</cfloop>

			<cfset local.Results = CollatedItemTypes>
			<cfcatch>
				<cfset local.Results = Duplicate(cfcatch)>
			</cfcatch>
		</cftry>

		<cfreturn local.Results>
	</cffunction>

	<cffunction name="GetMagicItem" access="remote" output="false" returntype="any">
		<cfargument name="ItemCategoryID" required="true" type="numeric"><!--- 1 = minor, 2 = medium, 3 = major --->
		<cfargument name="ItemTypeID" required="true" type="numeric">
		<cfargument name="Iterations" type="numeric" required="false" default="5000">

		<cfset local.CollatedItemTypes = {}>

		<cfif arguments.ItemTypeID eq 9>
			<cfset local.ItemCategoryID = 1>
		<cfelseif arguments.ItemTypeID eq 13>
			<cfset local.ItemCategoryID = 2>
		<cfelseif arguments.ItemTypeID eq 14>
			<cfset local.ItemCategoryID = 3>
		<cfelse>
			<cfset local.ItemCategoryID = arguments.ItemCategoryID>
		</cfif>

		<cftry>
			<cfloop from="1" to="#arguments.Iterations#" index="local.i">
				<cfquery name="local.GetItem" datasource="#this.DSN#">
					select mi.Name
					from MagicItems mi
						join magicitemsweightedlookup miwl on mi.id = miwl.ItemID
					where mi.ItemTypeID = #arguments.ItemTypeID#
						and mi.artifacttypeid is null
						and miwl.CategoryID = #local.ItemCategoryID#
					order by -LOG(1 - rand()) / miwl.weighted asc
					limit 1
				</cfquery>

				<cfif StructKeyExists(local.CollatedItemTypes, local.GetItem.Name)>
					<cfset local.CollatedItemTypes[local.GetItem.Name]++>
				<cfelse>
					<cfset local.CollatedItemTypes[local.GetItem.Name] = 1>
				</cfif>
			</cfloop>

			<cfset local.Results = CollatedItemTypes>
			<cfcatch>
				<cfset local.Results = Duplicate(cfcatch)>
			</cfcatch>
		</cftry>

		<cfreturn local.Results>
	</cffunction>

</cfcomponent>